package com.fh.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.fh.bo.GrantBo;
import com.fh.dao.PubDdvMapper;
import com.fh.dao.PurPurview2RoleMapper;
import com.fh.dao.PurPurviewMapper;
import com.fh.dao.PurUser2RoleMapper;
import com.fh.dao.SubSysParamMapper;
import com.fh.dto.PubDdv;
import com.fh.dto.PurPurview;
import com.fh.dto.PurPurview2Role;
import com.fh.dto.SubSysParam;
import com.fh.dto.SysUser;
import com.fh.web.constants.GlobalConstants;
import com.fh.web.service.PurviewService;
import com.fh.web.vo.PurviewVo;
import com.fh.web.vo.SubSysParamVo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 权限点管理的service接口实现类
 *
 */
@Service("purviewService")
public class PurviewServiceImpl implements PurviewService{
	
	@Resource
	private PurPurviewMapper purviewDtoMapper;
	@Resource
	private PurPurview2RoleMapper purPurview2RoleMapper;
	@Resource
	private PurUser2RoleMapper purUser2RoleMapper;
	@Resource
	private SubSysParamMapper subSysParamMapper;	
	@Resource
	private PubDdvMapper pubDdvMapper;

	@Override
	public List<PurviewVo> getPurviewAndOperation() {
		List<PurviewVo> list = new ArrayList<PurviewVo>();
		List<PurPurview> purviewList = purviewDtoMapper.selectAll();
		if(!purviewList.isEmpty()){
			for(PurPurview purPurview:purviewList){
				list.add(PurviewVo.getPurviewVo(purPurview));
			}
		}
		return list;
	}

	@Override
	public PurviewVo selectPurviewById(String pId) throws Exception {
		PurPurview purPurview = purviewDtoMapper.selectByPrimaryKey(Integer.parseInt(pId));
		PurviewVo purviewVo = new PurviewVo();
		if(null!=purPurview){
			BeanUtils.copyProperties(purPurview, purviewVo);
			purviewVo.setPurTypes(pubDdvMapper.selectRoleType(GlobalConstants.PUR_PURVIEW,GlobalConstants.PUR_TYPE));
		}
		return purviewVo;
	}

	@Override
	public boolean getPurviewByCode(PurviewVo PurviewVo) throws Exception {
		Example example = new Example(PurPurview.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("purCode", PurviewVo.getPurCode());
		if (PurviewVo.getPurId() != null && !"".equals(PurviewVo.getPurId())) {
			criteria.andNotEqualTo("purId", PurviewVo.getPurId());
		}
		List<PurPurview> purview = purviewDtoMapper.selectByExample(example);
		if (purview != null && purview.size()>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean getPurviewByName(PurviewVo PurviewVo) throws Exception {
		Example example = new Example(PurPurview.class);
		Criteria criteria = example.createCriteria();
		String purName=new String(PurviewVo.getPurName().getBytes("ISO-8859-1"),"utf-8");
		criteria.andEqualTo("purName", purName);
		if (PurviewVo.getPurId() != null && !"".equals(PurviewVo.getPurId())) {
			criteria.andNotEqualTo("purId", PurviewVo.getPurId());
		}
		List<PurPurview> purview = purviewDtoMapper.selectByExample(example);
		if (purview != null && purview.size()>0) {
			boolean boo =false;
			for(PurPurview purPurview:purview){
				if(purPurview.getParentPurId() == PurviewVo.getParentPurId()){
					boo = true;
					break;
				}
			}
			return boo;
		}
		return false;
	}

	@Override
	public void delPurview(Long rId) throws Exception {
		
		//删除用户权限点
		Example example = new Example(PurPurview2Role.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("purId", rId);
		purPurview2RoleMapper.deleteByExample(example);
		
		Example example1 = new Example(PurPurview.class);
		Criteria criteria1 = example1.createCriteria();
		criteria1.andEqualTo("purId",rId);
		List<PurPurview> list=purviewDtoMapper.selectByExample(example);
		
		Example exampleD = new Example(SubSysParam.class);
		Criteria criteriaD = exampleD.createCriteria();
		criteriaD.andEqualTo("purCode",list.get(0).getPurCode());
		subSysParamMapper.deleteByExample(exampleD);
		
		//删除权限表
		Example example2 = new Example(PurPurview.class);
		Criteria criteria2 = example2.createCriteria();
		criteria2.andEqualTo("purId",rId);
		purviewDtoMapper.deleteByExample(example2);
		
	}

	@Override
	public int isExcit(Long id) {
		Example example = new Example(PurPurview2Role.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("purId", id);
		List<PurPurview2Role> list = purPurview2RoleMapper.selectByExample(example);
		if(list.isEmpty()){
			return 0;
		}
		return 1;
	}

	@Override
	public void editPurView(PurviewVo purviewVo) throws Exception {
		PurPurview purview = new PurPurview();
		BeanUtils.copyProperties(purviewVo, purview);
		purviewDtoMapper.updateByPrimaryKeySelective(purview);
		//先检查purCode 是否发生变化如果变化修改 相关联表sub_sys_param
		PurPurview purview1=purviewDtoMapper.selectByPrimaryKey(purviewVo.getPurId());
		if(!purviewVo.getPurCode().equals(purview1.getPurCode())){
			Example example = new Example(SubSysParam.class);
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("purCode", purview1.getPurCode());
			List<SubSysParam> list=subSysParamMapper.selectByExample(example);
			SubSysParam subSysParam=list.get(0);
			subSysParam.setPurCode(purviewVo.getPurCode());
			subSysParamMapper.updateByPrimaryKey(subSysParam);
		}
		purviewDtoMapper.updateByPrimaryKeySelective(purviewVo);
		
	}
	
	@Override
	public void addPurview(PurviewVo purviewVo,SysUser sysStaff) throws Exception{
		if (purviewVo.getParentPurId() == null)
			purviewVo.setParentPurId(0);
		if (sysStaff != null) {
			purviewVo.setUserId(sysStaff.getUserId().intValue());
		}
		purviewVo.setCreateTime(new Date());
		purviewDtoMapper.insertSelective(purviewVo);
	}

	@Override
	public List<PurviewVo> getPurviewListNoChild(String pId) throws Exception {
		/** 查询所有权限点数据 */
		Example example = new Example(PurPurview.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("purType", 0);
		criteria.andNotEqualTo("purId", pId);
		criteria.andNotEqualTo("parentPurId", pId);
		List<PurPurview> purPurviews = purviewDtoMapper.selectByExample(example);
		List<PurviewVo> list = new ArrayList<PurviewVo>();
		if(!purPurviews.isEmpty()){
			for(PurPurview purPurview:purPurviews){
				list.add(PurviewVo.getPurviewVo(purPurview));
			}
		}
		return list;
	}
	
	@Override
	public int getPurviewListByid(Long parentPurid) throws Exception {
		Example example = new Example(PurPurview.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("parentPurId", parentPurid);
		criteria.andEqualTo("purType", 0);
		int childNum = purviewDtoMapper.selectByExample(example).size();
		return childNum;

	}

	@Override
	public List<PurviewVo> getPurviewList(Long pId) throws Exception {
		List<PurPurview> list = purviewDtoMapper.getRolePurviewList(pId);
		List<PurviewVo> listVo = new ArrayList<PurviewVo>();
		if(!list.isEmpty()){
			for(PurPurview purPurview:list){
				listVo.add(PurviewVo.getPurviewVo(purPurview));
			}
		}
		return listVo;
	}
	
	@Override
	public void addGrant(Long rId, String[] purIds, SysUser sysUser) throws Exception {
		// 先删除角色已有的权限
		Example example = new Example(PurPurview2Role.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roId", rId);
		purPurview2RoleMapper.deleteByExample(example);
		// 批量添加角色 权限点关系
		List<GrantBo> grantBoList = new ArrayList<GrantBo>();
		Date now = new Date();
		Long userId = null;
		if (sysUser != null)
			userId = sysUser.getUserId().longValue();
		if(purIds.length>0){
			for (int i = 0; i < purIds.length; i++) {
				GrantBo grantBo = new GrantBo();
				grantBo.setRoId(rId);
				grantBo.setPurId(Long.parseLong(purIds[i]));
				grantBo.setUserId(userId);
				grantBo.setCreateTime(now);
				grantBoList.add(grantBo);
			}

			purviewDtoMapper.addGrant(grantBoList);
		}
	}
	
	/**
	 * 查询所有权限点（操作权限点除外）
	 * 
	 */
	@Override
	public List<PurviewVo> getAllPurviewList1() throws Exception {
		/** 查询所有权限点数据 */
		Example example = new Example(PurPurview.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("purType", 0);
		List<PurPurview> purPurviews = purviewDtoMapper.selectByExample(example);
		List<PurviewVo> list = new ArrayList<PurviewVo>();
		if(!purPurviews.isEmpty()){
			for(PurPurview PurPurview:purPurviews){
				list.add(PurviewVo.getPurviewVo(PurPurview));
			}
		}
		return list;
	}
	

	@Override
	public SubSysParamVo getPurview(String purCode) throws Exception {
		Example example = new Example(SubSysParam.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("purCode",purCode);
		List<SubSysParam> list=subSysParamMapper.selectByExample(example);
		if(!list.isEmpty()){
			SubSysParamVo vo = new SubSysParamVo();
			BeanUtils.copyProperties(list.get(0), vo);
			return vo;
		}
		return null;
	}

	@Override
	public int addModule(SubSysParamVo vo) {
		return subSysParamMapper.insert(vo);
	}

	@Override
	public int updateModule(SubSysParamVo vo) {
		return subSysParamMapper.updateByPrimaryKeySelective(vo);
	}

	@Override
	public List<SubSysParamVo> getSubSysParam(Long userId) throws Exception {
		List<SubSysParam> list = subSysParamMapper.getSubSysParam(userId, GlobalConstants.TYPE);
		List<SubSysParamVo> listVo = new ArrayList<SubSysParamVo>();
		if(!list.isEmpty()){
			for(SubSysParam subSysParam:list){
				SubSysParamVo vo = new SubSysParamVo();
				BeanUtils.copyProperties(subSysParam, vo);
				listVo.add(vo);
			};
			
		}
		return listVo;
	}

	@Override
	public int checkSubSysSort(SubSysParamVo vo) {
		Example example = new Example(SubSysParam.class);
		Criteria criteria = example.createCriteria();
		if (null != vo.getId()) {
			criteria.andNotEqualTo("id", vo.getId());
		}
		criteria.andEqualTo("subSysSort", vo.getSubSysSort());
		List<SubSysParam> list = subSysParamMapper.selectByExample(example);
		if (list.isEmpty()) {
			return 0;
		}
		return 1;
	}

	@Override
	public List<PubDdv> purTypes() {
		return pubDdvMapper.selectRoleType(GlobalConstants.PUR_PURVIEW,GlobalConstants.PUR_TYPE);
	}
}
